<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回值</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="functions.arguments.html">« 函数的参数</a></li>
      <li style="float: right;"><a href="functions.variable-functions.html">可变函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.functions.html">函数</a></li>
    <li>返回值</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="functions.returning-values" class="sect1">
   <h2 class="title">返回值</h2>

   <p class="para">
    值通过使用可选的返回语句返回。可以返回包括数组和对象的任意类型。返回语句会立即中止函数的运行，并且将控制权交回调用该函数的代码行。更多信息见
    <span class="function"><a href="function.return.html" class="function">return</a></span>。
   </p>

   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     如果省略了
     <span class="function"><a href="function.return.html" class="function">return</a></span>，则返回值为 <strong><code>null</code></strong>。
    </p>
   </p></blockquote>

   <div class="sect2">
    <h3 class="title">return 的使用</h3>
    <p class="para">
     <div class="example" id="example-171">
     <p><strong>示例 #1 <span class="function"><a href="function.return.html" class="function">return</a></span> 的使用</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">square</span><span style="color: #007700">(</span><span style="color: #0000BB">$num</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$num&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">;<br />}<br />echo&nbsp;</span><span style="color: #0000BB">square</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;'16'。<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>

    <p class="para">
     函数不能返回多个值，但可以通过返回一个数组来得到类似的效果。
    </p>
    <p class="para">
     <div class="example" id="example-172">
      <p><strong>示例 #2 返回一个数组以得到多个返回值</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">small_numbers</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">];<br />}<br /></span><span style="color: #FF8000">//&nbsp;使用短数组语法将数组中的值赋给一组变量<br /></span><span style="color: #007700">[</span><span style="color: #0000BB">$zero</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$one</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$two</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">small_numbers</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;在&nbsp;7.1.0&nbsp;之前，唯一相等的选择是使用&nbsp;list()&nbsp;结构<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$zero</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$one</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$two</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">small_numbers</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="para">
     从函数返回一个引用，必须在函数声明和指派返回值给一个变量时都使用引用运算符 &amp;：
    </p>
    <p class="para">
     <div class="example" id="example-173">
      <p><strong>示例 #3 从函数返回一个引用</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;&amp;</span><span style="color: #0000BB">returns_reference</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$someref</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$newref&nbsp;</span><span style="color: #007700">=&amp;&nbsp;</span><span style="color: #0000BB">returns_reference</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="simpara">
    有关引用的更多信息, 请查看 <a href="language.references.html" class="link">引用的解释</a>。
   </p>
   </div>
  </div></div></div></body></html>